using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;

using WP.Commons;
using WP.DB;
using WP.MF.DataAccess;
using WP.MF.DataEntity;

namespace WP.MF.BusinessRule
{
    public class MUserBRL : RuleBase
    {

        public MUserBRL()
		{
			//
			// TODO: Add constructor logic here
			//
		}
        #region Insert MUser Valid
        public bool InsertValid(DBAccess dBAccess, MUserDEL mUserDEL)
        {
            bool bReturn = true;

            SysVar.ErrorMessage.Clear();

            int iDefMax = 1;
            string strMaxLoginNum = DataFunctions.GetFieldValue("msysMisc", "_Value", "_Name='" + BaseFunctions.EncryptDES("User_" + SysVar.UserName + "_MaxUsersNum", SysVar.EncryptKeys).Replace("'", "''") + "'").TrimEnd();
            if (strMaxLoginNum != "")
            {
                iDefMax = CvtFunc.ToInt(BaseFunctions.DecryptDES(strMaxLoginNum, SysVar.EncryptKeys));
                int iCount = CvtFunc.ToInt(DataFunctions.GetFieldValue("mUser", "Count(*)", "UserName<>'procom' and username<>'pro-com' and username<>'woodpro'"));
                if (iDefMax <= iCount)
                {
                    MsgBoxEx.Show("Over the maximum number of users.", SysVar.WarnTitle);
                    return false;
                }
            }

            if (FieldValueCheck(dBAccess, mUserDEL.Tables[0]))
            {
                bReturn = true;
            }
            else
            {
                bReturn = false;
            }
            return bReturn;
        }
        #endregion

        #region Update MUser Valid
        public bool UpdateValid(DBAccess dBAccess, MUserDEL mUserDEL)
        {
            bool bReturn = true;
            SysVar.ErrorMessage.Clear();

            if (IsExistedAndNotChanged(dBAccess, mUserDEL.Tables[MUserDEL.TABLENAME_MUSER]))
            {
                if (FieldValueCheck(dBAccess, mUserDEL.Tables[MUserDEL.TABLENAME_MUSER]))
                {
                    bReturn = true;
                }
                else
                {
                    bReturn = false;
                }
            }
            else
            {
                bReturn = false;
            }

            return bReturn;
        }

        #endregion

        #region Delete MUser Valid
        public bool DeleteValid(DBAccess dBAccess, string strRecId, byte[] btOldRecStamp)
        {
            SysVar.ErrorMessage.Clear();

            //the error was msgboxed, so just exit here
            if (!base.IsExistedAndNotChanged(dBAccess, MUserDEL.TABLENAME_MUSER, strRecId, btOldRecStamp))
            {
                return false;
            }

            if (MsgBoxEx.Show(Msg.Select("Are you sure to delete this user?", DataFunctions.GetMsg(this.GetType().Name, "MSG_DeleteThisUser")), SysVar.ConfirmTitle, MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2) == DialogResult.No)
            {
                return false;
            }

            string strUserName = DataFunctions.GetFieldValue(dBAccess, MUserDEL.TABLENAME_MUSER, "Username", "RecID = '" + strRecId + "'");
            if (strUserName.Trim().ToUpper() == SysVar.UserName.Trim().ToUpper())
            {
                MsgBoxEx.Show(Msg.Select("You cannot delete yourself.", DataFunctions.GetMsg(this.GetType().Name, "MSG_CanNotDeleteYourself")), SysVar.WarnTitle);
                return false;
            }
            return true;
        }

        #endregion

        #region Copy MUser Valid
        public bool CopyValid(DBAccess dBAccess, string strName, string strDesc)
        {
            SysVar.ErrorMessage.Clear();

            //the error was msgboxed, so just exit here
            if (strName == "")
            {
                MsgBoxEx.Show("User Name can not be empty!", SysVar.WarnTitle);
                return false;
            }
            else
            {
                if (DataFunctions.CheckRecordExist(dBAccess, MUserDEL.TABLENAME_MUSER, strName, "UserName"))
                {
                    MsgBoxEx.Show("User name already on file!", SysVar.WarnTitle);
                    return false;
                }
            }

            if (strDesc == "")
            {
                MsgBoxEx.Show("Description can not be empty!", SysVar.WarnTitle);
                return false;
            }

            return true;
        }
        #endregion

        #region Copy Field Check
        public bool CopyFieldCheck(DBAccess dbcon, string strFieldName, string strFieldValue, ref string strErrPrompt)
        {
            strErrPrompt = string.Empty;
            if (strFieldName == null) return true;

            switch (strFieldName.ToUpper().Trim())
            {
                #region Name
                case "NAME":
                    if (strFieldValue.Trim() == "")
                    {
                        strErrPrompt = "User Name can not be empty!";
                        return false;
                    }
                    else
                    {
                        if (DataFunctions.CheckRecordExist(dbcon, MUserDEL.TABLENAME_MUSER, strFieldValue, "UserName"))
                        {
                            strErrPrompt = "User name already on file!";
                            return false;
                        }
                    }
                    return true;
                #endregion
                #region Desc
                case "DESC":
                    if (strFieldValue.Trim() == "")
                    {
                        strErrPrompt = "Description can not be empty!";
                        return false;
                    }
                    return true;
                #endregion
                default:
                    return true;
            }
        }
        #endregion
    }
}
